<template>
    <div>
        <mian-layout @refresh="resetForm('search_data')">
            <template v-slot:conTop>
                <el-form size="mini" :inline="true" :model='search' ref="search_data" class="demo-form-inline search-form">
                    <el-form-item label="道具ID" prop="arPropId">
                        <el-input v-model="search.arPropId" type="number" placeholder="请输入道具ID"></el-input>
                    </el-form-item>
                    <el-form-item label="道具名称" prop="name">
                        <el-input v-model="search.name" placeholder="请输入道具名称"></el-input>
                    </el-form-item>
                    <el-form-item label="创建时间" prop="dataTime">
                        <el-date-picker v-model="dataTime" type="datetimerange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" value-format="yyyy-MM-dd HH:mm:ss" />
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" plain  icon="el-icon-search" @click="doSearch">搜索</el-button>
                        <el-button icon="el-icon-refresh-right" @click="resetForm('search_data')">重置</el-button>
                    </el-form-item>
                </el-form>   
            </template>
            <div class="btnBox" style="padding-bottom:10px">
                <el-button type="success" icon="el-icon-plus" size="mini" @click="goAdd">创建道具</el-button>
            </div>
            <my-table :loading="loading" :lists="tableData" @selectionChange="tableSelect" @pageChange="handleChange" :sequence="true" :page='page'>
                
                <el-table-column property="arPropId" label="道具ID" width="100" align='center'></el-table-column>
                <el-table-column property="adPlaceTitle" label="道具头图" align='center' width="150">
                    <template slot-scope="scope" align='center'>
                        <div class="imgBox">
                            <img :src="scope.row.headerImageUrl" alt=""/>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column property="name" label="道具名称" align='center'></el-table-column>
                <el-table-column property="nftName" label="关联NFT名称" align='center'></el-table-column>
                <el-table-column property="ownerAddress" label="持有人" align='center'></el-table-column>
                <el-table-column property="createTime" label="创建时间" width="180" align='center'></el-table-column>

                <el-table-column prop="operation" label="操作" width="280" align='center'>
                    <template slot-scope='scope'>
                        <el-button size="mini" icon="el-icon-search" @click="goLook(scope.row)">查看</el-button>
                        <el-button size="mini" type="danger" icon="el-icon-delete" @click="del(scope.row)">删除</el-button>
                        <el-button size="mini" type="success" plain icon="el-icon-edit" @click="goEdit(scope.row,2)">修改</el-button>
                    </template>
                </el-table-column>

            </my-table>
        </mian-layout>

    </div>
</template>
<script>
import {mapGetters} from "vuex"
import myTable from '@/components/public/myTable';
import u3dSceneSelect from "@/components/public/u3dSceneSelect.vue";
import {
  get_prop_list,
  del_prop
} from '@/api/collext'
export default {
    components:{
      myTable,
      u3dSceneSelect
    },
    data(){
        return{
            search:{
                arPropId:'',
                name:"",
                createTimeStrEnd:'',
                createTimeStrStart:''
            },
            dataTime:'',
            page:{
                pageSize:10,
                pageIndex:1,
			    total:0,
            },
            loading:true,
            tableData:[],
            multipleSelectionIds:[]
        }
    },
    computed:{
        ...mapGetters({
            'userInfo':'user/userInfo'
        })
    },
    created(){
        this.getList()
    },
    methods:{
        doSearch(){
            this.pageIndex = 1
            this.getList()
        },
        getList(callback){ 
            this.loading = true
            if(this.dataTime){
                this.search.createTimeStrStart = this.dataTime[0]
                this.search.createTimeStrEnd = this.dataTime[1]
            }
            get_prop_list({
                limit: this.page.pageSize,
                page: this.page.pageIndex,
                ...this.search
            }).then(res=>{
                this.loading = false
                this.tableData = res.data
                this.page.total = res.total
                callback && callback()
            })
        },
        del(item) {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                const res = await del_prop(item.arPropId)
                if(res.success){
                    this.$message.success('删除成功')
                    this.page.pageIndex = 1
                    this.getList()
                }else{
                   // this.$message.error('删除失败');
                }
            }).catch(() => {});
        },
        tableSelect(item){
            this.multipleSelectionIds = []
            for(let i=0;i<item.length;i++){
                this.multipleSelectionIds.push(item[i]._id)
            }
        },
        handleChange(index) {
            this.page.pageIndex = index;
            this.getList();
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.dataTime = []
            this.dataTime = []
            for (const key in this.search) {
               this.search[key] = '' 
            }
            this.page.pageIndex = 1
            this.getList()
        },
        goEdit(item,onlyLook){
            sessionStorage.setItem('propDetails',JSON.stringify(item))
            this.$store.commit('tagsView/SET_REGETDATA',this.getList)  // 设置需要刷新的方法，修改后返回需要刷新数据
            this.$router.push(`/industry/prop/add?id=${item.arPropId}`)
        },
        goAdd(){
            this.$store.commit('tagsView/SET_REGETDATA',this.getList)  // 设置需要刷新的方法，修改后返回需要刷新数据
            this.$router.push('/industry/prop/add')
        },
        goLook(item){
            sessionStorage.setItem('propDetails',JSON.stringify(item))
            this.$store.commit('tagsView/SET_REGETDATA',this.getList)  // 设置需要刷新的方法，修改后返回需要刷新数据
            this.$router.push(`/industry/prop/look/${item.arPropId}`)
        }
    }
}
</script>

<style lang="scss" scoped>
    .imgBox{
        display: flex;
        align-content: center;
        justify-content: center;
        img{
            display: block;
            max-width: 100px;
            max-height: 100px;
        }
    }
</style>